﻿using System;
using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Utils;
using RootMotion.FinalIK;
using UnityEngine;
using Random = System.Random;

namespace Gameplay.PVE.Survivor
{
    public class CollisionComponent: ComponentBase
    {
        private Vector3 currentPosition;
        private bool hasCollider;
        private CollisionManager.CollisionEntity entity;
        
        protected override void OnInitialized()
        {
            currentPosition = unit.Position;
            entity = CollisionManager.Instance.AddEntity(unit.unitId, currentPosition,unit.collisionRadius);
            hasCollider = true;
        }

        protected override void OnUpdate()
        {
            
        }

        public override void LateUpdate()
        {
            base.LateUpdate();
            if (!hasCollider)
            {
                return;
            }
            if (currentPosition != unit.Position)
            {
                currentPosition = unit.Position;
                entity.position = currentPosition;
                CollisionManager.Instance.MoveEntity(unit.unitId, currentPosition);

            }
        }

        protected override void OnDead()
        {
            base.OnDead();
            RemoveCollider();
        }

        public void RemoveCollider()
        {
            if (hasCollider)
            {
                hasCollider = false;
                CollisionManager.Instance.RemoveEntity(unit.unitId);
            }

        }

        public override void OnReset()
        {
            base.OnReset();
            RemoveCollider();
        }
    }
}
